import 'package:flutter/material.dart';

import 'pages/home.dart';
import 'pages/market.dart';
import 'pages/me.dart';

Color activeColor = const Color(0xFFFF7A3F);
Color unactiveColor = const Color(0xFF969696);

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
       home: new MainPageWidget());
  }
}
 
class MainPageWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MainPageState();
  }
}
 
class MainPageState extends State<MainPageWidget> {
  int _tabIndex = 0;
  var tabIcons;
  var appBarTitles = ['首页', '贷款超市', '我的'];
  /*
   * 存放三个页面，跟fragmentList一样
   */
  var _pageList;
 
  /*
   * 根据选择获得对应的normal或是press的icon
   */
  Icon getTabIcon(int curIndex) {
    if (curIndex == _tabIndex) {
      return tabIcons[curIndex][1];
    }
    return tabIcons[curIndex][0];
  }
  /*
   * 获取bottomTab的颜色和文字
   */
  Text getTabTitle(int curIndex) {
    if (curIndex == _tabIndex) {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(fontSize: 14.0, color: activeColor));
    } else {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(fontSize: 14.0, color: unactiveColor));
    }
  }
  /*
   * 根据image路径获取图片
   */
  // Image getTabImage(path) {
  //   return new Image.asset(path, width: 24.0, height: 24.0);
  // }
  // Image getTabImage(icon_name, icon_color) {
  //   return new Icon(Icons.${icon_name}, color: icon_color ),
  // }
 
  void initData() {
    /*
     * 初始化选中和未选中的icon
     */
    tabIcons = [
      [new Icon(Icons.home, color: unactiveColor ), new Icon(Icons.home, color: activeColor )],
      [new Icon(Icons.monetization_on, color: unactiveColor ), new Icon(Icons.monetization_on, color: activeColor )],
      [new Icon(Icons.perm_identity, color: unactiveColor ), new Icon(Icons.perm_identity, color: activeColor )]
    ];

    /*
     * 三个子界面
     */
    _pageList = [
      new Home(),
      new Market(),
      new Me(),
    ];
  }
 
  @override
  Widget build(BuildContext context) {
    //初始化数据
    initData();
    return Scaffold(
      body: _pageList[_tabIndex],
      bottomNavigationBar: new BottomNavigationBar(
        // fixedColor: Colors.green,
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(
            backgroundColor: Colors.blue,
            icon: getTabIcon(0), 
            title: getTabTitle(0)
          ),
          new BottomNavigationBarItem(
            backgroundColor: Colors.blue,
            icon: getTabIcon(1), 
            title: getTabTitle(1)
          ),
          new BottomNavigationBarItem(
            backgroundColor: Colors.blue,
            icon: getTabIcon(2), 
            title: getTabTitle(2)
          ),
        ],
        type: BottomNavigationBarType.fixed,
        //默认选中首页
        currentIndex: _tabIndex,
        iconSize: 24.0,
        //点击事件
        onTap: (index) {
          setState(() {
            _tabIndex = index;
          });
        },
      )
    );
  }
}